﻿using Compass.Wasm.Shared.Plans;
using Zack.DomainCommons.Models;

namespace Compass.PlanService.Domain.Entities;

public record Pallet : AggregateRootEntity, IAggregateRoot, IHasCreationTime, ISoftDelete
{
    //todo:将PackingItem中的信息查询后保存
    public Guid MainPlanId { get; init; } //关联装箱清单

    public int PalletNumber { get; private set; } //自增，从1开始
    //写入烟罩的冗余信息，产品编号，产品型号，产品长宽高，随着重新生成清单而修改
    public string? MtlNumber { get; private set; } //物料编码->产品编号
    public string? Type { get; private set; } //类型/产品型号
    public string? Length { get; private set; } //长
    public string? Width { get; private set; } //宽
    public string? Height { get; private set; } //高
    public bool ShowItem { get; private set; }//单独托盘显示物料信息，查询烟罩分段时设置为true，自定义物料时设定

    //生产现场填写信息
    public string? PalletLength { get; private set; } //包装长
    public string? PalletWidth { get; private set; } //包装宽
    public string? PalletHeight { get; private set; } //包装高
    public string? GrossWeight { get; private set; } //毛重
    public string? NetWeight { get; private set; } //净重
    public string? Remark { get; private set; }//备注


    //状态信息，状态必须查询出来，而不要存储
    public WarehouseLocation_e Location { get; private set; }
    public DateTime? WarehousingTime { get; private set; }
    public DateTime? ShippingTime { get; private set; }

    #region ctor
    private Pallet() { }
    //直接新增托盘
    public Pallet(Guid id, Guid mainPlanId, int palletNumber, string? mtlNumber,  string? type, string? length, string? width, string? height, string? palletLength, string? palletWidth, string? palletHeight, string? grossWeight, string? netWeight,bool showItem, string? remark)
    {
        Id=id;
        MainPlanId=mainPlanId;
        PalletNumber=palletNumber;
        MtlNumber = mtlNumber;
        Type=type;
        Length=length;
        Width=width;
        Height=height;
        PalletLength=palletLength;
        PalletWidth=palletWidth;
        PalletHeight = palletHeight;
        GrossWeight=grossWeight;
        NetWeight=netWeight;
        ShowItem=showItem;
        Remark=remark;
    }

    #endregion

    #region UpdatePallet
    public void Update(PalletDto dto)
    {
        ChangePalletNumber(dto.PalletNumber.Value)
            .ChangeMtlNumber(dto.MtlNumber)
            .ChangeType(dto.Type)
            .ChangeProductSize(dto.Length,dto.Width,dto.Height)
            .ChangePalletSize(dto.PalletLength,dto.PalletWidth,dto.PalletHeight)
            .ChangeGrossWeight(dto.GrossWeight)
            .ChangeNetWeight(dto.NetWeight)
            .ChangeShowItem(dto.ShowItem)
            .ChangeWarehousingTime(dto.WarehousingTime)
            .ChangeShippingTime(dto.ShippingTime)
            .ChangeLocation(dto.Location)
            .ChangeRemark(dto.Remark);
        NotifyModified();
    }
    public Pallet ChangePalletNumber(int palletNumber)
    {
        PalletNumber = palletNumber;
        return this;
    }
    public Pallet ChangeMtlNumber(string? mtlNumber)
    {
        MtlNumber = mtlNumber;
        return this;
    }
    public Pallet ChangeType(string? type)
    {
        Type=type;
        return this;
    }
    public Pallet ChangeProductSize(string? length, string? width, string? height)
    {
        Length=length;
        Width=width;
        Height=height;
        return this;
    }
    public Pallet ChangePalletSize(string? palletLength, string? palletWidth, string? palletHeight)
    {
        PalletLength = palletLength;
        PalletWidth = palletWidth;
        PalletHeight = palletHeight;
        return this;
    }
    
    public Pallet ChangeGrossWeight(string? grossWeight)
    {
        GrossWeight = grossWeight;
        return this;
    }
    public Pallet ChangeNetWeight(string? netWeight)
    {
        NetWeight = netWeight;
        return this;
    }
    public Pallet ChangeShowItem(bool showItem)
    {
        ShowItem = showItem;
        return this;
    }
    public Pallet ChangeRemark(string? remark)
    {
        Remark=remark;
        return this;
    }

    #endregion

    #region UpdateStatuses更新状态
    public void UpdateStatuses(PalletDto dto)
    {
        ChangeWarehousingTime(dto.WarehousingTime)
            .ChangeShippingTime(dto.ShippingTime)
            .ChangeLocation(dto.Location)
            .ChangeRemark(dto.Remark);
        NotifyModified();
    }

    public Pallet ChangeWarehousingTime(DateTime? warehousingTime)
    {
        WarehousingTime = warehousingTime;
        return this;
    }
    public Pallet ChangeShippingTime(DateTime? shippingTime)
    {
        ShippingTime = shippingTime;
        return this;
    }
    public Pallet ChangeLocation(WarehouseLocation_e location)
    {
        Location = location;
        return this;
    }


    #endregion
}